#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time: 2022/12/27 15:09
# @Author: ysy
# @Email: 928233621@qq.com
# @File：add_pages.py
from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy as By
from time import sleep
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class addPages:
    # 添加按钮定位器
    btn_add = (By.ID, "com.gfeng.daydaycook:id/iv_add")
    btn_add_szp = (By.XPATH, "//android.widget.FrameLayout/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]")
    btn_add_xsp = (By.XPATH, "//android.widget.FrameLayout/android.widget.FrameLayout[2]/android.widget.LinearLayout[1]")
    btn_add_cgx = (By.XPATH, "//android.widget.FrameLayout/android.widget.FrameLayout[3]/android.widget.LinearLayout[1]")
    # 选择图片定位器
    choice_img_locator = (By.ID, "com.gfeng.daydaycook:id/mTvIndex")
    # 选择图片中的下一步定位器
    btn_next_zp_locator = (By.ID, "com.gfeng.daydaycook:id/mTvNext")
    # 标题定位器
    text_title_locator = (By.ID, "com.gfeng.daydaycook:id/et_title")
    # 这一刻想说点什么，描述定位器
    text_edittext_locator = (By.ID, "com.gfeng.daydaycook:id/edittext")
    # 发布定位器
    btn_publish_locator = (By.ID, "com.gfeng.daydaycook:id/publish_textview")
    # 发布成功后的提示框中的二维码分享定位器
    toast_locator = (By.ID, "com.gfeng.daydaycook:id/tv_share_click")

    # 写食谱页面定位器
    # 菜谱封面定位器
    choice_cookbook_locator = (By.ID, "com.gfeng.daydaycook:id/rl_cover")
    # 菜谱菜名定位器
    text_dish_locator = (By.ID, "com.gfeng.daydaycook:id/et_recipe_name")
    # 菜的故事
    text_story_locator = (By.ID, "com.gfeng.daydaycook:id/et_recipe_description")
    # 菜谱中下一步进入编辑页面的定位器
    btn_next_sp_locator = (By.ID, "com.gfeng.daydaycook:id/tv_next")

    # 写食谱页面定位器
    # 食材定位器
    text_food_sp_locator = (By.ID, "com.gfeng.daydaycook:id/et_food_name")
    # 用量定位器
    text_num_sp_locator = (By.ID, "com.gfeng.daydaycook:id/et_num_unit")
    # 描述步骤定位器
    text_step_sp_locator = (By.ID, "com.gfeng.daydaycook:id/et_step_description")
    # 食谱分类定位器
    choice_class_sp_locator = (By.ID, "com.gfeng.daydaycook:id/nameTv")

    def __init__(self, driver):
        self.driver = driver

    # 点击添加按钮
    def click_add(self):
        self.driver.find_element(*self.btn_add).click()

    # 晒作品按钮
    def click_szp(self):
        self.driver.find_element(*self.btn_add_szp).click()

    # 写食谱按钮
    def click_xsp(self):
        self.driver.find_element(*self.btn_add_xsp).click()

    # 草稿箱按钮
    def click_cgx(self):
        self.driver.find_element(*self.btn_add_cgx).click()

    #选择图片
    def click_img(self):
        self.driver.find_element(*self.choice_img_locator).click()

    #点击下一步,连续点击两次
    def click_next(self):
        self.driver.find_element(*self.btn_next_zp_locator).click()

    #输入晒作品标题
    def input_title(self, value):
        # self.driver.find_element(*self.text_title_locator).click()
        self.driver.find_element(*self.text_title_locator).send_keys(value)

    #输入晒作品内容
    def input_content(self, value):
        self.driver.find_element(*self.text_edittext_locator).click()
        self.driver.find_element(*self.text_edittext_locator).send_keys(value)

    #点击发布
    def click_publish(self):
        self.driver.find_element(*self.btn_publish_locator).click()

    # 发布成功后等待并捕捉返回结果
    def publish_result(self):
        # 设置等待
        wait = WebDriverWait(self.driver, 10, 0.5)
        # 等待元素被加载
        element = wait.until(EC.presence_of_element_located(self.toast_locator))
        return element